<?php
namespace app\web\controller;

use app\model\Theme; 

class StuData extends Base
{
    public function Stu_data()
    {
global $_G, $_P;
$op = $_P['op'];
$module_name = MODULE_NAME;
$themeid = $_P['themeid'];
if($op=='entryStu'){
     //判断次数
    $text = $_P['text'];
    //主播
    $themeid = $_P['themeid'];
    $regMobile = '/[1][3-9]\d{9}/';
    preg_match_all($regMobile,$text,$result);
    $phone_array = [];
    if($result[0]){
        $phone_array = $result[0];
    }
    $insert_num = 0;   //添加数量
    $repeat_num = 0;   //重复数量
    if($phone_array){
        $time = time();
        foreach ($phone_array as $v){
            $enc_phone = self::encPhone($themeid,$v);
            //INSERT IGNORE 会忽略数据库中已经存在的数据，如果数据库没有数据，就插入新的数据，如果有数据的话就跳过这条数据
            $sql =  "INSERT IGNORE INTO ".tablename(t_stu_data)." (phone,themeid,addtime) VALUES ('{$enc_phone}','{$themeid}',{$time})";
            $insert = db_query($sql);
            if($insert){
                $insert_num++;
            }else{
                $repeat_num++;
            }
        }
    }
    $msg = '添加成功'.$insert_num.'条';
    if($repeat_num){
        $msg .= ',重复数据'.$repeat_num.'条';
    }
    return_json(0,$msg,array('total' => Theme::get_stu_data_total($themeid)));
}elseif($op=='delStu'){
    $del = db_delete(t_stu_data, array('themeid'=>$themeid));
    if($del){
        return_json(0,'清空成功');
    }else{
        return_json(0,'清空失败');
    }
}

function encPhone($themeid,$phone){
     return md5(MODULE_NAME.$themeid.$phone);
}
?>
}
}